<?php
class RoundedCorner {

    private $_image_path;
    private $_thumb_width;
    private $_thumb_height;
    private $_new_name;
    private $_to_image_path;
    private $_srcW;
    private $_srcH;
    private $_lanmu;
    public $filepath;

    function __construct($image_path,$width,$height='',$lanmu,$to_image_path = '') {
        $this->_image_path = $image_path;

        $this->_thumb_width = $width;
        $this->_thumb_height = $height;
        $this->_lanmu = $lanmu;

        if($to_image_path)
            $this->_to_image_path = $to_image_path;
        else
        {

            if(!is_dir(dirname(__FILE__).'/../../uploaded/'.$_SESSION['currentuser'].'/'.$this->_lanmu))
                mkdir(dirname(__FILE__).'/../../uploaded/'.$_SESSION['currentuser'].'/'.$this->_lanmu,'0777');

            $this->_to_image_path = dirname(__FILE__).'/../../uploaded/'.$_SESSION['currentuser'].'/'.$this->_lanmu.'/';
        }

        $this->_new_name = $this->_get_new_name();
    }

    public function get_new_width() {
        return $this->_thumb_width;
    }
    public function get_new_height() {
        return $this->_thumb_height;
    }
    public function filepath(){
    	return $this->filepath = 'http://'.$_SERVER['SERVER_NAME'].'/lyc/uploaded/'.$_SESSION['currentuser'].'/'.$this->_lanmu.$this->_get_new_name();
    }

    function makeThumbnail()
     {
        //初始化图片
        $srcImg = $this->_load_source_image();
	
        //取源图象的宽高
        $this->_get_source_size($srcImg);

        //计算比例，并给缩略图的宽和高重新赋值
        $this->_new_img_size($srcImg);
     }

     private function _new_img_size($srcImg) {
		
		
         if($this->_thumb_width > $this->_srcW || $this->_thumb_height > $this->_srcH )
         {
             if($this->_thumb_width == $this->_thumb_height)
             {
                $this->square($srcImg);
             }
             else
             {
                copy($this->_image_path,$this->_to_image_path.$this->_new_name);
                ImageDestroy($srcImg);
             }
         }

        else if($this->_thumb_width && $this->_thumb_height)
        {
            if($this->_thumb_width > $this->_thumb_height)
            {
                $this->_thumb_width = $this->radio($this->_srcW, $this->_thumb_height,$this->_srcH);
            }
            else if($this->_thumb_width == $this->_thumb_height)
            {
                $this->square($srcImg);
            }
            else
            {
                $this->_thumb_height = $this->radio($this->_srcH, $this->_thumb_width,$this->_srcW);
            }
            $this->_new_img($srcImg);
        }
        else if($this->_thumb_width == 0 || $this->_thumb_height == 0)
        {
            if($this->_thumb_width)
            {
                  $this->_thumb_height = $this->radio($this->_srcH, $this->_thumb_width,$this->_srcW);
            }
            else
            {
                $this->_thumb_width = $this->radio($this->_srcW, $this->_thumb_height,$this->_srcH);
            }
             $this->_new_img($srcImg);
        }
     }



    private function square($srcImg) {
        if($this->_srcW > $this->_srcH)
        {
            $sx = abs(($this->_srcH - $this->_srcW)/2);
            $sy = 0;
            $this->_srcW = $this->_srcH;
            $this->_srcH = $this->_srcH;
        }
        else
        {
            $sy = abs(($this->_srcW-$this->_srcH)/2);
            $sx = 0;
            $this->_srcW = $this->_srcW;
            $this->_srcH = $this->_srcW;


        }
        if(function_exists("imagecreatetruecolor"))
          $dim = imagecreatetruecolor($this->_thumb_width, $this->_thumb_height); // 创建目标图gd2
        else
          $dim = imagecreate($this->_thumb_width, $this->_thumb_height); // 创建目标图gd1

        imagecopyresized ($dim,$srcImg,0,0,$sx,$sy,$this->_thumb_width,$this->_thumb_height,$this->_srcW,$this->_srcH);

        imagejpeg($dim,$this->_to_image_path.$this->_new_name);
        imagedestroy($dim);
        die;
    }


     private function radio($gu,$j_o,$j_t) {

          $widthratio = $j_o/$j_t;
          return floor($gu *  $widthratio);
     }
     private function _new_img($srcImg) {

            if(function_exists("imagecopyresampled"))
            {
                $newim = imagecreatetruecolor($this->_thumb_width,$this->_thumb_height);
                imagecopyresampled($newim,$srcImg,0,0,0,0,$this->_thumb_width,$this->_thumb_height,$this->_srcW,$this->_srcH);
                //复制按比例缩放的原图到 ，新的黑色背景中。
            }
            else
            {
                $newim = imagecreate($this->_thumb_width,$this->_thumb_height);
                imagecopyresized($newim,$srcImg,0,0,0,0,$this->_thumb_width,$this->_thumb_height,$this->_srcW,$this->_srcH);
            }
            imagejpeg($newim,$this->_to_image_path.$this->_new_name);
            imagedestroy($newim);
     }

     private function _get_source_size($srcImg) {
         $this->_srcW = ImageSX($srcImg);
         $this->_srcH = ImageSY($srcImg);
     }

     public  function _get_new_name() {
        $newname = substr($this->_image_path,strrpos($this->_image_path,'/'));
        $newname = explode('.',$newname);
        $newname[0] = $newname[0].'_thumb';
        return $this->_new_name = implode('.',$newname);
     }

    private function _load_source_image() {
         $imgSize = GetImageSize($this->_image_path);
         $imgType = $imgSize[2];
         //@ 使函数不向页面输出错误信息
         switch ($imgType)
         {
            case 1:
                $srcImg = @ImageCreateFromGIF($this->_image_path);
                $this->_type = '.gif';
                break;
            case 2:
                $srcImg = @ImageCreateFromJpeg($this->_image_path);
                 $this->_type = '.jpg';
                break;
            case 3:
                $srcImg = @ImageCreateFromPNG($this->_image_path);
                $this->_type = '.png';
                break;
        }

        return $srcImg;
    }
}
